import os
import random

from gxl_ai_utils.utils import utils_file
from transformers.utils import has_file


def do_get_random_str_only_characters(length=16):
    """
    Generate a random string of given length.
    """
    import random
    import string
    return ''.join(random.choices(string.ascii_uppercase, k=length))
import shutil
def little_copy_file_list(dict_list, root_dir):
    """"""
    new_dict_list = []
    index = 0
    dir_name = str(index).zfill(12)
    # now_root_dir = os.path.join(root_dir, random_str)
    os.makedirs(root_dir, exist_ok=True)
    for dict_item in utils_file.tqdm(dict_list, desc='Copying files', total=len(dict_list)):
        # index += 1
        # if index % 100 == 0:
        #     random_str = do_get_random_str_only_characters()
        #     now_root_dir = os.path.join(root_dir, random_str)
        #     os.makedirs(now_root_dir, exist_ok=True)

        wav_path = dict_item['wav']
        output_path = os.path.join(root_dir, wav_path.split('/')[-1])
        # utils_file.copy_file(wav_path, output_path, use_shell=True)
        if os.path.exists(output_path):
            utils_file.logging_limit_print(f'Skipping {output_path}, already exists')
            continue
        try:
            shutil.copy2(str(wav_path), str(output_path))  # 保留mtime等元数据
        except Exception as e:
            print(f"Error: {wav_path} not found, skipping.,error: {e}")
            continue
        dict_item['wav'] = output_path
        new_dict_list.append(dict_item)
    return new_dict_list




def do_copy():
    runnser = utils_file.GXLMultiprocessingWithReturn(num_processes=200)
    jsonl_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/data.jsonl"
    root_dir = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/copy_opus"

    dict_list = utils_file.load_dict_list_from_jsonl(jsonl_path)
    random.shuffle(dict_list)
    result = runnser.run(little_copy_file_list, dict_list, root_dir=root_dir)
    new_dict_list = []
    for dict_item_list in result:
        new_dict_list.extend(dict_item_list)
    utils_file.write_dict_list_to_jsonl(new_dict_list, os.path.join(root_dir, "local_wav_data.jsonl"))


def do_show_finished_files():
    root_dir = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/copy_opus"
    wav_scp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav.scp"
    wav_dict = utils_file.load_dict_from_scp(wav_scp_path)
    has_finished_num = 0
    for wav_path in utils_file.tqdm(wav_dict, desc='checking files', total=len(wav_dict)):
        output_path = os.path.join(root_dir, wav_path.split('/')[-1])
        if os.path.exists(output_path):
            has_finished_num += 1
        if has_finished_num % 100 == 0:
            utils_file.logging_limit_print(f'Finished {has_finished_num} files')
    print(f'finished {has_finished_num} files')

def do_show_finished_file2():
    """"""
    root_dir = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/copy_opus"
    wav_dict = utils_file.do_get_scp_for_wav_dir(root_dir, suffix='.opus')
    print(f'finished {len(wav_dict)} files')
    # wav_scp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav_tmp.scp"
    # utils_file.write_dict_to_scp(wav_dict, wav_scp_path)
    # text_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/text.scp"
    # data_tmp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/data_tmp.jsonl"
    # utils_file.do_convert_wav_text_scp_to_jsonl(wav_scp_path, text_path, data_tmp_path)

def do_convert_jsonl_tmp():
    """"""
    wav_scp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav.scp"
    wav_dict = utils_file.load_dict_from_scp(wav_scp_path)
    print('wav_dict lens:', len(wav_dict))
    inverse_wav_dict = {}
    error_multi_key_file_names = set()
    for key, value in wav_dict.items():
        new_key_from_value = utils_file.get_file_pure_name_from_path(value)
        if new_key_from_value in inverse_wav_dict:
            print(f'Skipping {new_key_from_value}, already exists')
            error_multi_key_file_names.add(new_key_from_value)
            continue
        inverse_wav_dict[new_key_from_value] = key
    print(f'error_multi_key_file_names lens: {len(error_multi_key_file_names)}')
    for file_name in error_multi_key_file_names:
        inverse_wav_dict.pop(file_name)
    print('inverse_wav_dict lens:', len(inverse_wav_dict))
    mapping_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/mapping_filename2key.scp"
    utils_file.write_dict_to_scp(inverse_wav_dict, mapping_path)
    error_multi_key_file_names_file = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/error_multi_key_file_names.list"
    utils_file.write_list_to_file(list(error_multi_key_file_names), error_multi_key_file_names_file)
    wav_tmp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav_tmp.scp"
    wav_dict_little = utils_file.load_dict_from_scp(wav_tmp_path)
    print('wav_dict_little lens:', len(wav_dict_little))
    new_wav_dict_little = {}
    for key, value in wav_dict_little.items():
        new_key = inverse_wav_dict[key]
        new_wav_dict_little[new_key] = value
    wav_tmp_path_new = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav_tmp_new.scp"
    utils_file.write_dict_to_scp(new_wav_dict_little, wav_tmp_path_new)
    text_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/text.scp"
    data_jsonl_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/data_tmp.jsonl"
    utils_file.do_convert_wav_text_scp_to_jsonl(wav_tmp_path_new, text_path, data_jsonl_path)


def do_get_new_data_tmp():
    """"""
    mapping_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/mapping_filename2key.scp"
    mapping_dict = utils_file.load_dict_from_scp(mapping_path)
    root_dir = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/copy_opus"
    wav_dict = utils_file.do_get_scp_for_wav_dir(root_dir, suffix='.opus')
    new_wav_dict = {}
    for key, value in wav_dict.items():
        if key not in mapping_dict:
            continue
        new_key = mapping_dict[key]
        new_wav_dict[new_key] = value
    new_wav_scp_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/wav_tmp_new.scp"
    utils_file.write_dict_to_scp(new_wav_dict, new_wav_scp_path)
    text_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/text.scp"
    data_jsonl_path = "/mnt/apdcephfs_sgfd/share_303841515/Tealab/user/xuelonggeng/data/8W_asr_data_hq/data_tmp_stage2.jsonl"
    utils_file.do_convert_wav_text_scp_to_jsonl(new_wav_scp_path, text_path, data_jsonl_path)






if __name__ == '__main__':
    """"""


